#-------------------------------------------------------------------------------
# Name:        example_invoice1.py
# Purpose:     convert Kyocera example invoice (Prescribe) to simple PDF
#
# Author:      polejnik
#
# Version:     1.0
#
# Created:     8.11.2014
# Copyright:   (c) polejnik 2014
#-------------------------------------------------------------------------------
#!/usr/bin/env python

#-------------------------------------------------------------------------------
# Version history:
#-------------------------------------------------------------------------------
# version 1.0   - initial
#-------------------------------------------------------------------------------

import prescribe2pdf    # prescribe to PDF converter
import logging          # logging

def main():
    # initialize

    # logging
    sLogFile = 'example_invoice1.LOG'
    logging.basicConfig(filename=sLogFile, level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s')

    # set configuration file
    sConfFile = 'prescribe2pdf.ini'

    # set input invoice example
    sInvoiceFile = 'Invoice1.TXT'

    # read prescribe document
    prscrbDoc = prescribe2pdf.prescribeDocument()
    prscrbDoc.readSettings(sConfFile)

    # PDF
    prscrbDoc.openDocument(sInvoiceFile)
    prscrbDoc.readDocument()
    aTextLines = prscrbDoc.getDocumentLines()
    aFonts = prscrbDoc.getDocumentFonts()
    bHasLogo = prscrbDoc.getDocumentLogo()
    iLinesPerPage = prscrbDoc.getLinesPerPage()
    iPages = prscrbDoc.getTotalPages()
    prscrbDoc.closeDocument()

    # decode prescribe fonts to PDF fonts
    fontMapping = prescribe2pdf.prescribe2PdfFonts()
    fontMapping.readSettings(sConfFile)
    aFonts = fontMapping.convertFonts(aFonts)

    # generate PDF
    textPdf = prescribe2pdf.doTextPdf()
    textPdf.readSettings(sConfFile)
    textPdf.setPdfProperties(iPages, iLinesPerPage)
    textPdf.createTextPdf(sInvoiceFile+'.pdf')
    textPdf.writeTextPdf(aTextLines, aFonts, bHasLogo)
    textPdf.closeTextPdf()

    pass

if __name__ == '__main__':
    main()
